package model;

import java.sql.*;
import java.util.Date;
import dal.*;
import util.*;

public class AccountModel implements IModel {

	public static class FieldNames {
		public static String Username = "Username";
		public static String Password = "Password";
		public static String Status = "Status";
		public static String Email = "Email";
		public static String Type = "Type";

	}

	public class ErrorMessages {
		public String Username = "";
		public String Password = "";
		public String Status = "";
		public String Email = "";
		public String Type = "";

	}

	public ErrorMessages errMsgs = new ErrorMessages();

	public String[] errMsgList = null;

	// private variables
	private String _username = null;
	private String _password = null;
	private int _status = Integer.MIN_VALUE;
	private String _email = null;
	private int _type = Integer.MIN_VALUE;

	public AccountModel() {
	}

	// get/set
	public String getUsername() {
		return _username;
	}

	public void setUsername(String value) {
		_username = value;
	}

	public String getUsernameToString() {
		if (_username != null) {
			return HtmlUtil.genHtml(_username);
		} else {
			return "";
		}
	}

	public String getPassword() {
		return _password;
	}

	public void setPassword(String value) {
		_password = value;
	}

	public String getPasswordToString() {
		if (_password != null) {
			return HtmlUtil.genHtml(_password);
		} else {
			return "";
		}
	}

	public int getStatus() {
		return _status;
	}

	public void setStatus(int value) {
		_status = value;
	}

	public String getStatusToString() {
		if (_status == Integer.MIN_VALUE || _status == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_status);
		}
	}

	public String getEmail() {
		return _email;
	}

	public void setEmail(String value) {
		_email = value;
	}

	public String getEmailToString() {
		if (_email != null) {
			return HtmlUtil.genHtml(_email);
		} else {
			return "";
		}
	}

	public int getType() {
		return _type;
	}

	public void setType(int value) {
		_type = value;
	}

	public String getTypeToString() {
		if (_type == Integer.MIN_VALUE || _type == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_type);
		}
	}

	// added implement methods
	public void setCreator(int value) {

	}

	public void setId(int value) {

	}

	public Date getUpdated() {
		return null;
	}

	public void setUpdator(int value) {

	}

	public int getId() {
		return Integer.MIN_VALUE;
	}

	public String getName() {
		return "";
	}

	// end of added methods

	@Override
	public int[] getPrimaryKey() {
		int[] ids = new int[1];
		ids[0] = getId();
		return ids;
	}

	@Override
	public IModel readFromResultSet(ResultSet rs) throws SQLException {
		AccountModel model = null;

		model = new AccountModel();

		// read fields
		{
			String value = rs.getString("username");
			if (!rs.wasNull()) {
				model.setUsername(value);
			}
		}
		{
			String value = rs.getString("password");
			if (!rs.wasNull()) {
				model.setPassword(value);
			}
		}
		{
			int value = rs.getInt("status");
			if (!rs.wasNull()) {
				model.setStatus(value);
			}
		}
		{
			String value = rs.getString("email");
			if (!rs.wasNull()) {
				model.setEmail(value);
			}
		}
		{
			int value = rs.getInt("type");
			if (!rs.wasNull()) {
				model.setType(value);
			}
		}

		return model;
	}

	@Override
	public PreparedStatement genSelectStatement(Connection dbConn, IModel query)
			throws SQLException {
		String sql = "SELECT * FROM sp_account_select(:username);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModel obj = (AccountModel) query;

		// set parameters
		st.setObject("username", obj.getUsername());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genSelectListStatement(Connection dbConn,
			IModelExt query) throws SQLException {
		String sql = "SELECT * FROM sp_account_selectList(:limit, :offset, :username,:password,:status,:email,:type)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModelExt obj = (AccountModelExt) query;

		if (obj.getLimit() != Integer.MIN_VALUE) {
			st.setObject("limit", obj.getLimit());
		} else {
			st.setObject("limit", Integer.MAX_VALUE);
		}
		if (obj.getOffset() != Integer.MIN_VALUE) {
			st.setObject("offset", obj.getOffset());
		} else {
			st.setObject("offset", 0);
		}

		// set parameters
		if (obj.getUsername() != null) {
			st.setObject("username", obj.getUsername());
		} else {
			st.setObject("username", null);
		}
		if (obj.getPassword() != null) {
			st.setObject("password", obj.getPassword());
		} else {
			st.setObject("password", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getEmail() != null) {
			st.setObject("email", obj.getEmail());
		} else {
			st.setObject("email", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genInsertStatement(Connection dbConn, IModel model)
			throws SQLException {
		String sql = "SELECT sp_account_insert(:username,:password,:status,:email,:type)";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModel obj = (AccountModel) model;

		// set parameters here
		if (obj.getUsername() != null) {
			st.setObject("username", obj.getUsername());
		} else {
			st.setObject("username", null);
		}
		if (obj.getPassword() != null) {
			st.setObject("password", obj.getPassword());
		} else {
			st.setObject("password", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getEmail() != null) {
			st.setObject("email", obj.getEmail());
		} else {
			st.setObject("email", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genUpdateStatement(Connection dbConn, IModel model)
			throws SQLException {
		String sql = "SELECT sp_account_update(:username,:password,:status,:email,:type)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModel obj = (AccountModel) model;

		// set parameters here
		if (obj.getUsername() != null) {
			st.setObject("username", obj.getUsername());
		} else {
			st.setObject("username", null);
		}
		if (obj.getPassword() != null) {
			st.setObject("password", obj.getPassword());
		} else {
			st.setObject("password", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getEmail() != null) {
			st.setObject("email", obj.getEmail());
		} else {
			st.setObject("email", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genDeleteStatement(Connection dbConn, IModel query)
			throws SQLException {
		String sql = "select sp_account_delete(:username);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModel obj = (AccountModel) query;

		// set primary keys
		st.setObject("username", obj.getUsername());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genCountStatement(Connection dbConn,
			IModelExt input) throws SQLException {
		String sql = "SELECT sp_account_count(:username,:password,:status,:email,:type)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		AccountModelExt obj = (AccountModelExt) input;

		// set parameters here
		if (obj.getUsername() != null) {
			st.setObject("username", obj.getUsername());
		} else {
			st.setObject("username", null);
		}
		if (obj.getPassword() != null) {
			st.setObject("password", obj.getPassword());
		} else {
			st.setObject("password", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getEmail() != null) {
			st.setObject("email", obj.getEmail());
		} else {
			st.setObject("email", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}

		return st.getPreparedStatement();
	}

	public int CheckLogin(String name, String pass) throws SQLException {
		int ireturn = 0;
		// Temporarily fix
		if (name == null || pass == null)
			return 2;

		if (name.equals("admin") && pass.equals("admin"))
			ireturn = 0;
		else if (name.equals("user") && pass.equals("user"))
			ireturn = 1;
		else
			ireturn = 2;
		/*
		 * AccountModel acc = new AccountModel(); GenericDal<AccountModel,
		 * AccountModelExt> dal = new GenericDal<AccountModel,
		 * AccountModelExt>(acc); AccountModel query = new AccountModel();
		 * 
		 * query.setUsername(name); query.setPassword(pass); AccountModel obj =
		 * null; try { obj = dal.select(query); }catch(Exception e) {
		 * e.printStackTrace(); //Error connection } if(name == null) { ireturn
		 * = 1; //Don't exist username in session }else if(obj == null) {
		 * ireturn = 2; //Don't exist wrong username or password }else { ireturn
		 * = 0; //right condition }
		 */
		return ireturn;
	}
	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":username", HtmlUtil.genHtml(_username)) .replace(":password",
	 * HtmlUtil.genHtml(_password)) .replace(":status",
	 * Integer.toString(_status)) .replace(":email", HtmlUtil.genHtml(_email))
	 * .replace(":type", Integer.toString(_type))
	 * 
	 * ; return html; }
	 */

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":Username", HtmlUtil.genHtml(_Username)) .replace(":Password",
	 * HtmlUtil.genHtml(_Password)) .replace(":Status",
	 * Integer.toString(_Status)) .replace(":Email", HtmlUtil.genHtml(_Email))
	 * .replace(":Type", Integer.toString(_Type))
	 * 
	 * ; return html; }
	 */

}